package com.str.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.str.dao.*;
import com.str.dto.*;
import com.str.pojo.*;
import com.str.utils.PageUtils;
import com.str.utils.PoiUtils;
import com.str.vo.GenIndividualLocusVO;
import com.str.service.KitInfoService;
import com.str.utils.CommonUtil;
import com.str.utils.CopyUtils;
import com.str.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 试剂盒(KitInfo)表服务实现类
 *
 * @author makejava
 * @since 2023-04-15 11:44:05
 */
@Service("kitInfoService")
@Slf4j
public class KitInfoServiceImpl implements KitInfoService {

    @Resource
    private KitInfoDao kitInfoDao;

    @Resource
    private DataSplitingDao dataSplitingDao;

    @Resource
    private IndividualLocusDao individualLocusDao;

    @Resource
    private LocusDao locusDao;

    @Resource
    private KitLocusDao kitLocusDao;

    /**
     * 分页查询基因数据列表
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryKitInfoList(QueryKitInfoListDto dto, HttpServletRequest request) {
        List<QueryKitInfoListVO> list = new ArrayList<>();
        //PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<KitInfo> resultLists = kitInfoDao.selectList(new QueryWrapper<KitInfo>()
                .like(StringUtil.isNotEmpty(dto.getKitName()), "kit_name", dto.getKitName())
                .eq(Objects.nonNull(dto.getComNumber()), "com_number", dto.getComNumber())
                .eq(Objects.nonNull(dto.getSamplingType()), "sampling_type", dto.getSamplingType())
                .eq("kit_status", 1).orderBy(true,false,"update_time"));
        if (Objects.nonNull(resultLists) && resultLists.size() > 0) {
            list = CopyUtils.copyList(resultLists, QueryKitInfoListVO.class);
        }
        return new ResponseVO(PageUtils.startPage(list, dto.getPageNum(), dto.getPageSize()));
    }

    /**
     * 基因详情数据查询(编辑数据回显)
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryDetailsKitInfo(QueryDetailsKitInfoDto dto, HttpServletRequest request) {
        log.info("【基因详情数据查询】，请求参数为：{}", JSON.toJSONString(dto));
        QueryDetailsKitInfoVO vo = new QueryDetailsKitInfoVO();
        //1、查询试剂盒信息
        KitInfo kitInfo = kitInfoDao.selectOne(new QueryWrapper<KitInfo>()
                .eq("kit_id", dto.getKitId())
                .eq("kit_status", 1));
        if (Objects.nonNull(kitInfo)) {
            log.info("【基因详情数据查询】====>[查询试剂盒信息]，响应结果为：{}", JSON.toJSONString(kitInfo));
            vo = CopyUtils.copyObj(kitInfo, QueryDetailsKitInfoVO.class);
        } else {
            throw new RuntimeException("未查询到该试剂盒信息！");
        }
        //2、查询基因座信息(基因座和实力个体数据)
        List<GenInfoVO> genInfoVOS = kitInfoDao.selectLocusAndIndividualByKitId(dto.getKitId(),null);
        if (Objects.nonNull(genInfoVOS) && genInfoVOS.size() > 0) {
            for (GenInfoVO genInfoVO : genInfoVOS) {
                //3、根据基因座id查询等位基因、峰面积信息
                List<DataSpliting> dataSplitings = dataSplitingDao.selectList(new QueryWrapper<DataSpliting>().eq("locus_id", genInfoVO.getLocusId()));
                if (Objects.nonNull(dataSplitings) && dataSplitings.size() > 0) {
                    List<AlleleVO> list = CopyUtils.copyList(dataSplitings, AlleleVO.class);
                    genInfoVO.setAlleleVoList(list);
                }
                //4、根据基因座id查询示例个体结果信息
                List<IndividualLocus> individualLocusList = individualLocusDao.selectList(new QueryWrapper<IndividualLocus>().eq("locus_id", genInfoVO.getLocusId()));
                if (Objects.nonNull(individualLocusList)) {
                    List<GenIndividualLocusVO> genotypes = CopyUtils.copyList(individualLocusList, GenIndividualLocusVO.class);
                    genInfoVO.setGenotypes(genotypes);
                }
            }
        } else {
            log.info("查询基因座信息(基因座和实力个体数据)未查询到数据！！！");
        }
        vo.setLocusVoList(genInfoVOS);
        return new ResponseVO(vo);
    }

    /**
     * 新增、修改基因数据
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVO saveOrUpdateKitInfo(SaveOrUpdateKitInfoDto dto, HttpServletRequest request) {
        log.info("【新增、修改基因数据】===>传入请求参数为：{}", JSONObject.toJSONString(dto));
        //获取登录用户信息
        SystemUserVO loginUser = CommonUtil.getSystemUser(request);
        if (StringUtil.isNotEmpty(dto.getKitId())) {
            log.info("【新增、修改基因数据】===>【修改操作】");
            //修改操作
            //1、更新试剂盒数据
            KitInfo kitInfo = CopyUtils.copyObj(dto, KitInfo.class);
            kitInfo.setUpdateBy(loginUser.getUserName());
            kitInfo.setUpdateTime(new Date());
            kitInfoDao.update(kitInfo, new QueryWrapper<KitInfo>().eq("kit_id", dto.getKitId()));
            if (Objects.nonNull(dto.getList()) && dto.getList().size() > 0) {
                for (GenInfoVO genInfoVO : dto.getList()) {
                    if (StringUtil.isNotEmpty(genInfoVO.getLocusId())) {
                        //更新基因座信息
                        Locus locus = CopyUtils.copyObj(genInfoVO, Locus.class);
                        locusDao.update(locus, new QueryWrapper<Locus>().eq("locus_id", genInfoVO.getLocusId()));
                        if (Objects.nonNull(genInfoVO.getAlleleVoList()) && genInfoVO.getAlleleVoList().size() > 0) {
                            for (AlleleVO alleleVO : genInfoVO.getAlleleVoList()) {
                                if (StringUtil.isNotEmpty(alleleVO.getSplitId())) {
                                    //更新等位基因、峰面积信息
                                    DataSpliting dataSpliting = CopyUtils.copyObj(alleleVO, DataSpliting.class);
                                    dataSpliting.setUpdateBy(loginUser.getUserName());
                                    dataSpliting.setUpdateTime(new Date());
                                    dataSplitingDao.update(dataSpliting, new QueryWrapper<DataSpliting>().eq("split_id", alleleVO.getSplitId()));
                                } else {
                                    //新增等位基因、峰面积信息
                                    DataSpliting dataSpliting = CopyUtils.copyObj(alleleVO, DataSpliting.class);
                                    dataSpliting.setSplitId(CommonUtil.uuid());
                                    dataSpliting.setLocusId(genInfoVO.getLocusId());
                                    dataSpliting.setCreatedBy(loginUser.getUserName());
                                    dataSpliting.setUpdateBy(loginUser.getUserName());
                                    dataSplitingDao.insert(dataSpliting);
                                }
                            }
                        }
                        if (Objects.nonNull(genInfoVO.getGenotypes()) && genInfoVO.getGenotypes().size() > 0) {
                            for (GenIndividualLocusVO genotype : genInfoVO.getGenotypes()) {
                                if (StringUtil.isNotEmpty(genotype.getIndId())) {
                                    //更新个体示例数据
                                    IndividualLocus individualLocus = CopyUtils.copyObj(genotype, IndividualLocus.class);
                                    individualLocus.setUpdateBy(loginUser.getUserName());
                                    individualLocus.setUpdateTime(new Date());
                                    log.info("【新增、修改基因数据】===>【更新】个体示例数据，封装后的数据为：{}",JSONObject.toJSONString(individualLocus));
                                    individualLocusDao.update(individualLocus, new QueryWrapper<IndividualLocus>().eq("ind_id", genotype.getIndId()));
                                } else {
                                    //新增个体示例数据
                                    IndividualLocus individualLocus = CopyUtils.copyObj(genotype, IndividualLocus.class);
                                    individualLocus.setIndId(CommonUtil.uuid());
                                    individualLocus.setLocusId(genInfoVO.getLocusId());
                                    individualLocus.setUpdateBy(loginUser.getUserName());
                                    individualLocus.setCreatedBy(loginUser.getUserName());
                                    //新增前做重复校验
                                    IndividualLocus sqlIndividualLocus = individualLocusDao.selectOne(new QueryWrapper<IndividualLocus>()
                                            .eq("locus_id", individualLocus.getLocusId()).eq("genotype", individualLocus.getGenotype()));
                                    if (Objects.isNull(sqlIndividualLocus)){
                                        log.info("【新增、修改基因数据】===>【新增】前做重复校验，封装后的数据为：{}",JSONObject.toJSONString(individualLocus));
                                        individualLocusDao.insert(individualLocus);
                                    }
                                }
                            }
                        }
                    } else {
                        //新增基因座信息
                        Locus locus = CopyUtils.copyObj(genInfoVO, Locus.class);
                        String locusId = CommonUtil.uuid();
                        locus.setLocusId(locusId);
                        locus.setCreatedBy(loginUser.getUserName());
                        locus.setUpdateBy(loginUser.getUserName());
                        locusDao.insert(locus);
                        //新增试剂盒和基因座关系表数据
                        KitLocus kitLocus = new KitLocus();
                        kitLocus.setKitId(dto.getKitId());
                        kitLocus.setLocusId(locusId);
                        kitLocusDao.insert(kitLocus);
                        //新增等位基因、峰面积信息
                        if (Objects.nonNull(genInfoVO.getAlleleVoList()) && genInfoVO.getAlleleVoList().size() > 0) {
                            for (AlleleVO alleleVO : genInfoVO.getAlleleVoList()) {
                                DataSpliting dataSpliting = CopyUtils.copyObj(alleleVO, DataSpliting.class);
                                dataSpliting.setSplitId(CommonUtil.uuid());
                                dataSpliting.setLocusId(locusId);
                                dataSpliting.setCreatedBy(loginUser.getUserName());
                                dataSpliting.setUpdateBy(loginUser.getUserName());
                                dataSplitingDao.insert(dataSpliting);
                            }
                        }
                        //新增示例个体数据信息
                        if (Objects.nonNull(genInfoVO.getGenotypes()) && genInfoVO.getGenotypes().size() > 0) {
                            for (GenIndividualLocusVO genotype : genInfoVO.getGenotypes()) {
                                IndividualLocus individualLocus = CopyUtils.copyObj(genotype, IndividualLocus.class);
                                individualLocus.setIndId(CommonUtil.uuid());
                                individualLocus.setLocusId(locusId);
                                individualLocus.setCreatedBy(loginUser.getUserName());
                                individualLocus.setUpdateBy(loginUser.getUserName());
                                //新增前做重复校验
                                IndividualLocus sqlIndividualLocus = individualLocusDao.selectOne(new QueryWrapper<IndividualLocus>()
                                        .eq("locus_id", individualLocus.getLocusId()).eq("genotype", individualLocus.getGenotype()));
                                if (Objects.isNull(sqlIndividualLocus)){
                                    log.info("【新增、修改基因数据】===>【新增】个体示例数据，封装后的数据为：{}",JSONObject.toJSONString(individualLocus));
                                    individualLocusDao.insert(individualLocus);
                                }
                            }
                        }
                    }

                }
            }
        } else {
            log.info("【新增、修改基因数据】===>【添加操作】");
            //添加操作
            //添加试剂盒信息
            KitInfo kitInfo = CopyUtils.copyObj(dto, KitInfo.class);
            String kitId = CommonUtil.uuid();
            kitInfo.setKitId(kitId);
            kitInfo.setUpdateBy(loginUser.getUserName());
            kitInfo.setCreatedBy(loginUser.getUserName());
            kitInfoDao.insert(kitInfo);
            //添加基因座信息
            if (Objects.nonNull(dto.getList()) && dto.getList().size() > 0) {
                for (GenInfoVO genInfoVO : dto.getList()) {
                    Locus locus = CopyUtils.copyObj(genInfoVO, Locus.class);
                    String locusId = CommonUtil.uuid();
                    locus.setLocusId(locusId);
                    locus.setUpdateBy(loginUser.getUserName());
                    locus.setCreatedBy(loginUser.getUserName());
                    locusDao.insert(locus);
                    //新增试剂盒和基因座关系表数据
                    KitLocus kitLocus = new KitLocus();
                    kitLocus.setKitId(kitId);
                    kitLocus.setLocusId(locusId);
                    kitLocusDao.insert(kitLocus);
                    //新增等位基因、峰面积信息
                    if (Objects.nonNull(genInfoVO.getAlleleVoList()) && genInfoVO.getAlleleVoList().size() > 0) {
                        for (AlleleVO alleleVO : genInfoVO.getAlleleVoList()) {
                            DataSpliting dataSpliting = CopyUtils.copyObj(alleleVO, DataSpliting.class);
                            dataSpliting.setSplitId(CommonUtil.uuid());
                            dataSpliting.setLocusId(locusId);
                            dataSpliting.setCreatedBy(loginUser.getUserName());
                            dataSpliting.setUpdateBy(loginUser.getUserName());
                            dataSplitingDao.insert(dataSpliting);
                        }
                    }
                    //新增示例个体数据信息
                    if (Objects.nonNull(genInfoVO.getGenotypes()) && genInfoVO.getGenotypes().size() > 0) {
                        for (GenIndividualLocusVO genotype : genInfoVO.getGenotypes()) {
                            IndividualLocus individualLocus = CopyUtils.copyObj(genotype, IndividualLocus.class);
                            individualLocus.setIndId(CommonUtil.uuid());
                            individualLocus.setLocusId(locusId);
                            individualLocus.setCreatedBy(loginUser.getUserName());
                            individualLocus.setUpdateBy(loginUser.getUserName());
                            //新增前做重复校验
                            IndividualLocus sqlIndividualLocus = individualLocusDao.selectOne(new QueryWrapper<IndividualLocus>()
                                    .eq("locus_id", individualLocus.getLocusId()).eq("genotype", individualLocus.getGenotype()));
                            if (Objects.isNull(sqlIndividualLocus)){
                                log.info("【新增、修改基因数据】===>【新增】个体示例数据，封装后的数据为：{}",JSONObject.toJSONString(individualLocus));
                                individualLocusDao.insert(individualLocus);
                            }
                        }
                    }
                }
            }
        }
        return new ResponseVO("操作成功！");
    }

    /**
     * 删除基因数据
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO deleteKitInfo(QueryDetailsKitInfoDto dto, HttpServletRequest request) {
        //获取登录用户信息
        SystemUserVO loginUser = CommonUtil.getSystemUser(request);
        //采用软删除策略
        KitInfo kitInfo = kitInfoDao.selectOne(new QueryWrapper<KitInfo>().eq("kit_id", dto.getKitId()));
        if (Objects.nonNull(kitInfo)) {
            List<KitLocus> kitLocusList = kitLocusDao.selectList(new QueryWrapper<KitLocus>().eq("kit_id", dto.getKitId()));
            if (Objects.nonNull(kitLocusList) && kitLocusList.size() > 0) {
                for (KitLocus kitLocus : kitLocusList) {
                    kitLocusDao.delete(new QueryWrapper<KitLocus>().eq("kit_id", kitLocus.getKitId()));
                    Locus locus = new Locus();
                    locus.setLocusStatus(0);
                    locus.setUpdateBy(loginUser.getUserName());
                    locus.setUpdateTime(new Date());
                    locusDao.update(locus, new QueryWrapper<Locus>().eq("locus_id", kitLocus.getLocusId()));
                }
            }
            //软删除检测盒数据
            KitInfo deleteKitInfo = new KitInfo();
            deleteKitInfo.setKitStatus(0);
            deleteKitInfo.setUpdateTime(new Date());
            deleteKitInfo.setUpdateBy(loginUser.getUserName());
            kitInfoDao.update(deleteKitInfo, new QueryWrapper<KitInfo>().eq("kit_id", kitInfo.getKitId()));
        } else {
            throw new RuntimeException("不存在该试剂盒信息！");
        }
        return new ResponseVO("删除成功！");
    }

    /**
     * 基因详情数据查询(编辑数据回显)-根据基因座名称模糊查询等位基因、峰面积及其示例个体数据信息
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryDataByLocusName(QueryDataByLocusNameDto dto, HttpServletRequest request) {
        //2、查询基因座信息(基因座和实力个体数据)
        List<GenInfoVO> genInfoVOS = kitInfoDao.selectLocusAndIndividualByKitId(dto.getKitId(),dto.getLocusName());
        if (Objects.nonNull(genInfoVOS) && genInfoVOS.size() >0) {
            for (GenInfoVO genInfoVO : genInfoVOS) {
                //3、根据基因座id查询等位基因、峰面积信息
                List<DataSpliting> dataSplitings = dataSplitingDao.selectList(new QueryWrapper<DataSpliting>().eq("locus_id",genInfoVO.getLocusId()));
                if (Objects.nonNull(dataSplitings) && dataSplitings.size()>0){
                    List<AlleleVO> alleleVOList = CopyUtils.copyList(dataSplitings,AlleleVO.class);
                    genInfoVO.setAlleleVoList(alleleVOList);
                }
                //4、根据基因座id查询示例个体结果信息
                List<IndividualLocus> individualLocusList = individualLocusDao.selectList(new QueryWrapper<IndividualLocus>().eq("locus_id", genInfoVO.getLocusId()));
                if (Objects.nonNull(individualLocusList) && individualLocusList.size()==2){
                    List<GenIndividualLocusVO> genotypes = CopyUtils.copyList(individualLocusList, GenIndividualLocusVO.class);
                    genInfoVO.setGenotypes(genotypes);
                }
            }
        }else {
            log.info("查询基因座信息(基因座和实力个体数据)未查询到数据！！！");
        }
        return new ResponseVO(PageUtils.startPage(genInfoVOS, dto.getPageNum(), dto.getPageSize()));
    }

    /**
     * 导入Excel基因数据
     *
     * @param file
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVO importKitInfo(MultipartFile file, HttpServletRequest request) throws Exception {
        //获取登录用户信息
        SystemUserVO loginUser = CommonUtil.getSystemUser(request);
        //依据模板配置标题行数、表头行数，读取Excel数据为java对象
        List<ImportKitInfoDto> importControlObjectDtoList = PoiUtils.importExcel(file, 2, 2, ImportKitInfoDto.class);
        log.info("从Excel导入数据一共 {} 行 ", importControlObjectDtoList.size());
        //存储试剂盒信息
        if (Objects.nonNull(importControlObjectDtoList) && importControlObjectDtoList.size() > 0) {
            for (ImportKitInfoDto importKitInfoDto : importControlObjectDtoList) {
                //存储试剂盒信息
                KitInfo kitInfo = CopyUtils.copyObj(importKitInfoDto, KitInfo.class);
                String kitId = CommonUtil.uuid();
                kitInfo.setKitId(kitId);
                kitInfo.setCreatedBy(loginUser.getUserName());
                kitInfo.setUpdateBy(loginUser.getUserName());
                //存储前判断是否已经存在
                KitInfo sqlResult = kitInfoDao.selectOne(new QueryWrapper<KitInfo>()
                        .eq("kit_name", importKitInfoDto.getKitName())
                        .eq("com_number", importKitInfoDto.getComNumber())
                        .eq("sampling_type", importKitInfoDto.getSamplingType())
                        .eq("source", importKitInfoDto.getSource()));
                if (Objects.isNull(sqlResult)) {
                    kitInfoDao.insert(kitInfo);
                } else {
                    kitId = sqlResult.getKitId();
                }
                //存储基因座信息
                if (Objects.nonNull(importKitInfoDto.getList()) && importKitInfoDto.getList().size() > 0) {
                    for (ImportLocusInfoDto importLocusInfoDto : importKitInfoDto.getList()) {
                        Locus locus = CopyUtils.copyObj(importLocusInfoDto, Locus.class);
                        String locusId = CommonUtil.uuid();
                        locus.setLocusId(locusId);
                        locus.setCreatedBy(loginUser.getUserName());
                        locus.setUpdateBy(loginUser.getUserName());
                        //存储前以该试剂盒id查询该试剂盒下是否已存在该基因座信息
                        Locus sqlLocus = locusDao.queryLocusByKitId(kitId, locus.getLocusName());
                        if (Objects.isNull(sqlLocus)) {
                            locusDao.insert(locus);
                        } else {
                            locusId = sqlLocus.getLocusId();
                        }
                        //存储基因座和试剂盒中间表
                        KitLocus kitLocus = new KitLocus();
                        kitLocus.setKitId(kitId);
                        kitLocus.setLocusId(locusId);
                        //存储之前判断是否已经存在该数据
                        KitLocus sqlKitLocus = kitLocusDao.selectOne(new QueryWrapper<KitLocus>()
                                .eq("kit_id", kitLocus.getKitId()).eq("locus_id", kitLocus.getLocusId()));
                        if (Objects.isNull(sqlKitLocus)) {
                            kitLocusDao.insert(kitLocus);
                        }
                        //存储等位基因和峰面积信息
                        DataSpliting dataSpliting = CopyUtils.copyObj(importLocusInfoDto, DataSpliting.class);
                        dataSpliting.setSplitId(CommonUtil.uuid());
                        dataSpliting.setLocusId(locusId);
                        dataSpliting.setCreatedBy(loginUser.getUserName());
                        dataSpliting.setUpdateBy(loginUser.getUserName());
                        //存储判断是否已存在该信息
                        DataSpliting sqlDataSpliting = dataSplitingDao.selectOne(new QueryWrapper<DataSpliting>()
                                .eq("locus_id", locusId)
                                .eq("allele", importLocusInfoDto.getAllele())
                                .eq("peak_area", importLocusInfoDto.getPeakArea()));
                        if (Objects.isNull(sqlDataSpliting)) {
                            dataSplitingDao.insert(dataSpliting);
                        }
                        //存储个体示例数据
                        if (StringUtil.isNotEmpty(importLocusInfoDto.getGenotypeOne())) {
                            importIndividualLocus(locusId, importLocusInfoDto.getGenotypeOne(), loginUser);
                        }
                        if (StringUtil.isNotEmpty(importLocusInfoDto.getGenotypeTwo())) {
                            importIndividualLocus(locusId, importLocusInfoDto.getGenotypeTwo(), loginUser);
                        }
                        if (StringUtil.isNotEmpty(importLocusInfoDto.getGenotypeThree())) {
                            importIndividualLocus(locusId, importLocusInfoDto.getGenotypeThree(), loginUser);
                        }
                    }
                }
            }
        }
        return new ResponseVO("导入成功！");
    }

    /**
     * 导出基因数据
     *
     * @param dto
     * @param request
     * @return
     */
    @Override
    public ResponseVO exportKitInfo(ExportKitInfoDto dto, HttpServletRequest request, HttpServletResponse response) {
        List<ExportKitInfoVO> vos = new ArrayList<>();
        List<KitInfo> kitInfos = kitInfoDao.selectList(new QueryWrapper<KitInfo>()
                .like(StringUtil.isNotEmpty(dto.getKitName()), "kit_name", dto.getKitName())
                .eq(Objects.nonNull(dto.getComNumber()), "com_number", dto.getComNumber())
                .eq(Objects.nonNull(dto.getSamplingType()), "sampling_type", dto.getSamplingType())
                .eq("kit_status", 1));
        if (Objects.nonNull(kitInfos) && kitInfos.size() > 0) {
            for (KitInfo kitInfo : kitInfos) {
                ExportKitInfoVO vo = CopyUtils.copyObj(kitInfo, ExportKitInfoVO.class);
                //查询基因座信息
                List<ExportLocusInfoVO> list = new ArrayList<>();
                List<KitLocus> kitLocusList = kitLocusDao.selectList(new QueryWrapper<KitLocus>().eq("kit_id", kitInfo.getKitId()));
                if (Objects.nonNull(kitLocusList) && kitLocusList.size() > 0) {
                    for (KitLocus kitLocus : kitLocusList) {
                        Locus locus = locusDao.selectOne(new QueryWrapper<Locus>()
                                .eq("locus_id", kitLocus.getLocusId())
                                .eq("locus_status", 1));
                        List<DataSpliting> dataSplitings = dataSplitingDao.selectList(new QueryWrapper<DataSpliting>()
                                .eq("locus_id", kitLocus.getLocusId()));
                        List<IndividualLocus> individualLocus = individualLocusDao.selectList(new QueryWrapper<IndividualLocus>()
                                .eq("locus_id", kitLocus.getLocusId()));
                        if (Objects.nonNull(dataSplitings) && dataSplitings.size() > 0) {
                            for (DataSpliting dataSpliting : dataSplitings) {
                                ExportLocusInfoVO exportLocusInfoVO = CopyUtils.copyObj(dataSpliting, ExportLocusInfoVO.class);
                                if (Objects.nonNull(individualLocus) && individualLocus.size() == 1 && Objects.nonNull(locus)) {
                                    exportLocusInfoVO.setLocusName(locus.getLocusName());
                                    exportLocusInfoVO.setGenotypeOne(StringUtil.isNotEmpty(individualLocus.get(0).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                }
                                if (Objects.nonNull(individualLocus) && individualLocus.size() == 2 && Objects.nonNull(locus)) {
                                    exportLocusInfoVO.setLocusName(locus.getLocusName());
                                    exportLocusInfoVO.setGenotypeOne(StringUtil.isNotEmpty(individualLocus.get(0).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                    exportLocusInfoVO.setGenotypeTwo(StringUtil.isNotEmpty(individualLocus.get(1).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                }
                                if (Objects.nonNull(individualLocus) && individualLocus.size() == 3 && Objects.nonNull(locus)) {
                                    exportLocusInfoVO.setLocusName(locus.getLocusName());
                                    exportLocusInfoVO.setGenotypeOne(StringUtil.isNotEmpty(individualLocus.get(0).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                    exportLocusInfoVO.setGenotypeTwo(StringUtil.isNotEmpty(individualLocus.get(1).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                    exportLocusInfoVO.setGenotypeThree(StringUtil.isNotEmpty(individualLocus.get(2).getGenotype()) ?
                                            individualLocus.get(0).getGenotype() : null);
                                }
                                list.add(exportLocusInfoVO);
                            }
                        }
                    }
                }
                vo.setList(list);
                vos.add(vo);
            }
        } else {
            throw new RuntimeException("未查询到数据！");
        }
        List<ExportKitInfoVO> collect = vos.stream().sorted(Comparator.comparing(ExportKitInfoVO::getUpdateTime)).collect(Collectors.toList());
        PoiUtils.exportExcel(collect, "STR基因"+(Objects.nonNull(dto.getKitName())?dto.getKitName():"")+"信息表", "sheet1", ExportKitInfoVO.class, "", response);
        return null;
    }

    /**
     * 获取已存在的试剂盒名称集合
     * @param request
     * @return
     */
    @Override
    public ResponseVO queryKitNameList(HttpServletRequest request) {
        List<String> kitNameList = new ArrayList<>();
        List<KitInfo> kitInfos = kitInfoDao.selectList(new QueryWrapper<KitInfo>().eq("kit_status", 1));
        if (Objects.nonNull(kitInfos) && kitInfos.size()>0){
            for (KitInfo kitInfo : kitInfos) {
                kitNameList.add(kitInfo.getKitName());
            }
        }
        return new ResponseVO(kitNameList);
    }

    /**
     * 导入数据存储个体示例数据
     *
     * @param locusId
     * @param genotype
     */
    public void importIndividualLocus(String locusId, String genotype, SystemUserVO loginUser) {
        IndividualLocus individualLocus = new IndividualLocus();
        individualLocus.setIndId(CommonUtil.uuid());
        individualLocus.setLocusId(locusId);
        individualLocus.setGenotype(genotype);
        individualLocus.setCreatedBy(loginUser.getUserName());
        individualLocus.setUpdateBy(loginUser.getUserName());
        IndividualLocus sqlIndividualLocus = individualLocusDao.selectOne(new QueryWrapper<IndividualLocus>()
                .eq("locus_id", locusId).eq("genotype", individualLocus.getGenotype()));
        if (Objects.isNull(sqlIndividualLocus)) {
            individualLocusDao.insert(individualLocus);
        }
    }
}
